package org.abettor.angleworm.ui.widget;

import java.io.IOException;

import org.abettor.angleworm.i18n.Translator;
import org.abettor.util.Environment;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * 联系人标签页
 * @author abettor
 */
public class ContactComposite extends Composite
{
	private static Translator translator = null;
	private Display display = Display.getDefault();
	private Label labelFilter = null;
	private Text textFilter = null;
	private Button buttonFilter = null;
	private Tree contactTree = null;

	/**
	 * 构造联系人标签页
	 * @param parent
	 * @param style
	 */
	public ContactComposite(TabFolder parent, int style)
	{
		super(parent, style);
		initialize();
	}

	private void initialize()
	{
		try
		{
			translator = Translator.getInstance();
			if(translator == null)
			{
				translator = Translator.initialize("en_US");
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}

		GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.numColumns = 3;
		gridLayout.horizontalSpacing = 0;
		setLayout(gridLayout);

		labelFilter = new Label(this, SWT.SHADOW_NONE);
		labelFilter.setText(translator.getString("main.tab.contact.filter.label"));
		labelFilter.setLayoutData(new GridData());

		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		textFilter = new Text(this, SWT.BORDER);
		textFilter.setForeground(display.getSystemColor(SWT.COLOR_GRAY));
		textFilter.setLayoutData(gridData);
		textFilter.setText(translator.getString("main.tab.contact.filter.text"));
		textFilter.setToolTipText(translator.getString("main.tab.contact.filter.tooltip"));

		buttonFilter = new Button(this, SWT.NONE);
		buttonFilter.setLayoutData(new GridData());
		//buttonFilter.setText(translator.getString("main.tab.contact.filter.button"));
		buttonFilter.setImage(new Image(display, Environment.getFilePath("img/search.png")));
		buttonFilter.setToolTipText(translator.getString("main.tab.contact.filter.tooltip"));

		createContactTree();

		buttonFilter.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(textFilter.getText().equals("") || translator.getString("main.tab.contact.filter.text").equals(textFilter.getText()))
				{
					textFilter.setFocus();
					return;
				}

				//TODO	从列表中过过滤符合条件的用户信息
			}
		});
		textFilter.addFocusListener(new FocusAdapter()
		{
			public void focusGained(FocusEvent e)
			{
				textFilter.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
				textFilter.setText("");
			}

			public void focusLost(FocusEvent e)
			{
				if(textFilter.getText().equals("") || translator.getString("main.tab.contact.filter.text").equals(textFilter.getText()))
				{
					textFilter.setForeground(display.getSystemColor(SWT.COLOR_GRAY));
					textFilter.setText(translator.getString("main.tab.contact.filter.text"));
				}
			}
		});
	}

	private void createContactTree()
	{
		GridData gridDataTree = new GridData();
		gridDataTree.grabExcessHorizontalSpace = true;
		gridDataTree.grabExcessVerticalSpace = true;
		gridDataTree.horizontalAlignment = GridData.FILL;
		gridDataTree.verticalAlignment = GridData.FILL;
		gridDataTree.horizontalSpan = 3;
		contactTree = new Tree(this, SWT.BORDER);
		contactTree.setLayoutData(gridDataTree);

		//TODO	显示分组列表
		for(int i = 0; i < 3; i ++)
		{
			TreeItem item = new TreeItem(contactTree, SWT.NONE);
			item.setText("Group " + i);
			item.setImage(new Image(display, Environment.getFilePath("img/group.png")));

			//TODO	显示联系人列表
			for(int j = 0; j < 5; j ++)
			{
				TreeItem subItem = new TreeItem(item, SWT.NONE);
				subItem.setText("Contact " + i + "_" + j);
				subItem.setImage(new Image(display, Environment.getFilePath("img/contact.png")));
			}
		}
	}
}
